package com.yanqu.road.dao.impl.activity.summergames;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.summergames.cross.CrossSummerGamesUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSummerGamesUserDataDaoImpl extends TempDataObjectDao<CrossSummerGamesUserData> {

    public static String getCrossSummerGamesUserDataSql = "select * from `t_c_summer_games_user_data` where `activityId`=?";
    public Map<Long, CrossSummerGamesUserData> getCrossSummerGamesUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossSummerGamesUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossSummerGamesUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSummerGamesUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossSummerGamesUserDataSql = "update `t_c_summer_games_user_data` set receiveSet=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossSummerGamesUserData crossSummerGamesUserData) {
        crossSummerGamesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesUserData.getReceiveSet()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossSummerGamesUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSummerGamesUserDataSql = "insert into `t_c_summer_games_user_data`(activityId,userId,receiveSet) values (?,?,?)";
    @Override
    protected boolean addData(CrossSummerGamesUserData crossSummerGamesUserData) {
        crossSummerGamesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSummerGamesUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSummerGamesUserData.getReceiveSet()));
        if (getDb().executeNoneQuery(insertCrossSummerGamesUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSummerGamesUserData parseData(ResultSet rs) throws SQLException {
        CrossSummerGamesUserData crossSummerGamesUserData = new CrossSummerGamesUserData();
        crossSummerGamesUserData.setActivityId(rs.getInt("activityId"));
        crossSummerGamesUserData.setUserId(rs.getLong("userId"));
        crossSummerGamesUserData.setReceiveSet(rs.getInt("receiveSet"));

        return crossSummerGamesUserData;
    }
}
